<?php include '../common/session.php' ?>
<?php include '../common/header.php' ?>
<?php
	if( $_SESSION['userType'] == 'less' ){
		$url = $_SERVER['HTTP_REFERER'];
		header('location:'.$url);
	}
	$active = $_GET['active'];
	$id = $_GET['id'];
	
	if( $active == 'edit' ){
		include "../common/connect.php";
		$result = mysql_query("SELECT * FROM pic WHERE id='".$id."'");
		mysql_close($con);
		$row = mysql_fetch_array($result);
		if( !is_array($row) ){
			$url = $_SERVER['HTTP_REFERER'];
			header('location:'.$url);
		}
	}
?>
<body>
<?php include "menus.php" ?>
<div id="main">
	<h2><?php if( $active == 'edit' ){ ?>编辑<?php }else{ ?>添加<?php } ?>图片<a href="picAdmin.php">[返回]</a></h2>
	<div class="addPic">
		<?php if( $active == 'edit' ){ ?>
		<p>图片效果：</p>
		<div class="preViewCon"><img src="../<?php echo $row['path'] ?>" alt="" /></div>
		<?php } ?>
		<form action="addPicForm.php?id=<?php echo $id ?>" method="post" enctype="multipart/form-data" target="ifr">
			<input type="hidden" name="MAX_FILE_SIZE" value="200000" />
			<?php if( $active == 'add' ){ ?>
			<label>
				<span>图片选择：</span>
				<input name="file" type="file" value="" />
			</label>
			<?php } ?>
			<label>
				<span>图片名称：</span>
				<input type="text" name="imgName" value="<?php if( $active == 'edit' ){ echo $row['imgName']; } ?>" />
			</label>
			<label>
				<span>图片类型：</span>
				<select name="type">
					<option <?php if( $active == 'edit' && $row['type'] == 'default' ){ ?> selected="selected"<?php } ?> value="default">默认图片</option>
					<option <?php if( $active == 'edit' && $row['type'] == 'banner' ){ ?> selected="selected"<?php } ?> value="banner">Banner图片</option>
				</select>
			</label>
			<label>
				<span>图片描述：</span>
				<textarea name="description"><?php if( $active == 'edit' ){ echo $row['description']; } ?></textarea>
			</label>
			<a href="javascript:void(0);" class="sub"><?php if( $active == 'edit' ){ ?>修 改<?php }else{ ?>上 传<?php } ?></a>
		</form>
	</div>
	<iframe name="ifr" style="display:none;"></iframe>
</div>
<script type="text/javascript">
function uploaded(res){
	var msg = '';
	switch(res){
		case 0:
			msg = '图片上传成功!';
			break;
		case 1:
			msg = '上传失败，图片超出大小!';
			break;
		case 2:
			msg = '上传失败，图片格式错误！';
			break;
		case 3:
			msg = '图片上传失败，请重新上传!';
			break;
		case 4:
			msg = '修改成功!';
			break;
		default:
			msg = '出现未知原因错误，请重试!';
			break;
	}
	Dir.popClose();
	Dir.popOpen({
		tit : '图片上传',
		content : '<div style="padding:15px;">'+msg+'</div>',
		cancelBtn : false,
		closeCallback : function(){
			if( res == 0 || res == 4 ){
				window.location.href = 'picAdmin.php';
				return false;
			}
		},
		sureCallback : function(){
			Dir.popClose();
			if( res == 0 || res == 4 ){
				window.location.href = 'picAdmin.php';
				return false;
			}
		}
	});
}
</script>
<script type="text/javascript">
$(document).ready(function(){
	$('a.sub').click(function(){
		Dir.popOpen({
			tit : '图片上传',
			content : '<div style="padding:20px;"><img src="../misc/img/ajax-loader.gif" alt="" /></div>',
			confirm : false,
			closeBtn : false
		});
		$('form').submit();
	});
});
</script>
</body>
</html>